package jdk.thread.threadpool;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorServiceDemo {

    public static void main(String[] args) {
        ScheduledExecutorService service1 = Executors.newScheduledThreadPool(10);

        service1.execute(new DemoRunnable("即时任务"));



        /**
         * 连续执行之间的时期
         * 看清楚Task是Runnable接口的，如果到本次执行时上次执行还没有完成则跳过本执行，因为Task是Runnable,如果是Callable就不清楚了
         */
        service1.scheduleAtFixedRate(new DemoRunnable("固定速率"), 2000l, 1000l, TimeUnit.MILLISECONDS);
        /**
         *  一个执行终止与下一个执行的开始之间的延迟
         */
        service1.scheduleWithFixedDelay(new DemoRunnable("固定延迟"), 2000l, 1000l, TimeUnit.MILLISECONDS);

    }
}

class DemoRunnable implements Runnable{

    private String name;

    public DemoRunnable(String name){
        this.name = name;
    }



    public void run() {
        System.out.println("执行任务["+name+"]:"+new Date());
        try {
            Thread.sleep(300l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
